package com.zlt.controller.seeker;

import com.zlt.controller.BaseController;
import com.zlt.dto.OnlineResumeDTO;
import com.zlt.service.ResumeService;
import com.zlt.service.impl.ResumeServiceImpl;
import com.zlt.util.ClassUtil;
import com.zlt.util.ProxyCreator;
import com.zlt.util.QnyUtil;
import com.zlt.util.StringUtil;
import com.zlt.vo.OnlineResumeVO;
import com.zlt.vo.Result;
import com.zlt.vo.ResumeVO;
import com.zlt.vo.UserVo;

import javax.servlet.ServletException;
import javax.servlet.annotation.MultipartConfig;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.Part;
import java.awt.image.BufferedImage;
import java.io.*;
import java.net.URL;
import java.net.URLConnection;
import java.util.List;

import static com.zlt.util.PdfConvertHtmlUtil.*;

@WebServlet("/seeker/resume/*")
@MultipartConfig
public class ResumeController extends BaseController {

    private ResumeService resumeService = (ResumeService) ProxyCreator.createProxy(new ResumeServiceImpl());

    @Override
    protected void defaultMethod(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

    }

    /**
     * 创建在线简历
     *
     * @param req
     * @param resp
     * @return
     * @throws ServletException
     * @throws IOException
     */
    protected Result createOnlineResume(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        OnlineResumeDTO onlineResumeDTO = ClassUtil.resolveRequest(OnlineResumeDTO.class, req);
        try {
            boolean success = resumeService.createOnlineResume(onlineResumeDTO);
            return success ? Result.ok("操作成功") : Result.error("操作失败");

        } catch (RuntimeException e) {
            e.printStackTrace();
            return Result.error(e.getMessage());
        }

    }


    /**
     * 修改个人优势
     *
     * @param req
     * @param resp
     * @return
     * @throws ServletException
     * @throws IOException
     */
    protected Result updateAdvantage(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String advantage = req.getParameter("advantage");
        try {
            boolean success = resumeService.updateAdvantage(advantage);
            return success ? Result.ok("操作成功") : Result.error("操作失败");
        } catch (RuntimeException e) {
            e.printStackTrace();
            return Result.error(e.getMessage());
        }

    }

    /**
     * 修改期望职位
     *
     * @param req
     * @param resp
     * @return
     * @throws ServletException
     * @throws IOException
     */
    protected Result updateExpectedPosition(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String expectedPosition = req.getParameter("expectedPosition");
        try {
            boolean success = resumeService.updateExpectedPosition(expectedPosition);
            return success ? Result.ok("操作成功") : Result.error("操作失败");
        } catch (RuntimeException e) {
            e.printStackTrace();
            return Result.error(e.getMessage());
        }
    }

    /**
     * 修改学历
     *
     * @param req
     * @param resp
     * @return
     * @throws ServletException
     * @throws IOException
     */

    protected Result updateEducation(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String education = req.getParameter("education");
        try {
            boolean success = resumeService.updateEducation(education);
            return success ? Result.ok("操作成功") : Result.error("操作失败");
        } catch (RuntimeException e) {
            e.printStackTrace();
            return Result.error(e.getMessage());
        }
    }


    /**
     * 修改实习经历
     *
     * @param req
     * @param resp
     * @return
     * @throws ServletException
     * @throws IOException
     */

    protected Result updateInternship(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String internship = req.getParameter("internship");
        try {
            boolean success = resumeService.updateInternship(internship);
            return success ? Result.ok("操作成功") : Result.error("操作失败");
        } catch (RuntimeException e) {
            e.printStackTrace();
            return Result.error(e.getMessage());
        }
    }

    /**
     * 修改期望薪资
     *
     * @param req
     * @param resp
     * @return
     * @throws ServletException
     * @throws IOException
     */

    protected Result updateSalary(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String minSalary = req.getParameter("minSalary");
        String maxSalary = req.getParameter("maxSalary");
        try {
            boolean success = resumeService.updateSalary(minSalary, maxSalary);
            return success ? Result.ok("操作成功") : Result.error("操作失败");
        } catch (RuntimeException e) {
            e.printStackTrace();
            return Result.error(e.getMessage());
        }
    }

    /**
     * 查看在线简历
     * @param req
     * @param resp
     * @return
     * @throws ServletException
     * @throws IOException
     */
    protected Result queryOnlineResume(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        try {
            OnlineResumeVO onlineResumeVO = resumeService.queryOnlineResume();
            return onlineResumeVO !=null ? Result.ok("操作成功" , onlineResumeVO) : Result.error("操作失败");
        } catch (RuntimeException e ){
            e.printStackTrace();
            return Result.error(e.getMessage());
        }
    }

    /**
     * 查看简历列表
     *
     * @param req
     * @param resp
     * @return
     * @throws ServletException
     * @throws IOException
     */
    protected Result queryAllResume(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        try {
            List<ResumeVO> resumeVOS = resumeService.queryAllResume();
            return Result.ok("成功", resumeVOS);
        } catch (RuntimeException e) {
            e.printStackTrace();
            return Result.error("失败");
        }
    }

    /**
     * 查询用户信息
     * @param req
     * @param resp
     * @return
     * @throws ServletException
     * @throws IOException
     */
    protected Result queryPhoneEmail(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        UserVo userVo = resumeService.getUSerByUid();
        return userVo != null ? Result.ok("操作成功",userVo) : Result.error("操作失败");
    }

    /**
     * 删除某个简历
     * @param req
     * @param resp
     * @return
     * @throws ServletException
     * @throws IOException
     */
    protected Result removeResume(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String rid = req.getParameter("rid");
        try {
            boolean success = resumeService.deleteResume(Integer.parseInt(rid));
            return success ? Result.ok("删除成功") : Result.error("删除失败");
        } catch (RuntimeException e) {
            e.printStackTrace();
        }
        return Result.error("删除失败");
    }

    /**
     * 预览某个简历
     * @param req
     * @param resp
     * @return
     * @throws ServletException
     * @throws IOException
     */
    protected Result previewResume(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        URL url = null;
        URLConnection con = null;
        String rid = req.getParameter("rid");
        try {
            ResumeVO resumeVO = resumeService.previewResume(Integer.parseInt(rid));
//            System.out.println(resumeVO.getPath());
            url = new URL(resumeVO.getPath());
            con = url.openConnection();
            InputStream inputStream = null;
            BufferedImage bufferedImage = null;
            try {
                inputStream = con.getInputStream();
                bufferedImage = pdfStreamToPng(inputStream);
//                String base64_png = bufferedImageToBase64(bufferedImage);
//                createHtmlByBase64(base64_png,htmlPath);
//                System.out.println("data:image/png;base64," + base64_png);
//                resumeVO.setPath("data:image/png;base64," + base64_png);
            } catch (Exception e) {
                e.printStackTrace();
            }finally {
                try {
                    if(inputStream != null){
                        inputStream.close();
                    }
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            return Result.ok("预览成功",resumeVO);
        } catch (RuntimeException e) {
            e.printStackTrace();
            return Result.error("预览失败");
        }
    }

    /**
     * 设置默认简历
     * @param req
     * @param resp
     * @return
     * @throws ServletException
     * @throws IOException
     */
    protected Result setupDefaultResume(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String rid = req.getParameter("rid");
        System.out.println(rid);
        try {
            boolean success1 = resumeService.setupDefaultResumeToNormal();
            if (success1 == true) {
                boolean success2 = resumeService.setupDefaultResume(Integer.parseInt(rid));
                return success2 ? Result.ok("设置默认简历成功") : Result.error("设置默认简历失败");
            } else {
                return Result.error("设置默认简历失败");
            }
        } catch (RuntimeException e) {
            e.printStackTrace();
        }
        return Result.error("设置默认简历失败");
    }

    /**
     * 发送简历
     * @param req
     * @param resp
     * @return
     * @throws ServletException
     * @throws IOException
     */
    protected Result sendResume(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String cid = req.getParameter("cid");
        String jid = req.getParameter("jid");
        try {
            boolean success = resumeService.sendResume(cid,jid);
            return success ? Result.ok("投递成功") : Result.error("投递失败");
        } catch (RuntimeException e) {
            e.printStackTrace();
        }
        return Result.error("投递失败");
    }

    /**
     * 上传简历附件
     * @param req
     * @param resp
     * @return
     * @throws ServletException
     * @throws IOException
     */
    protected Result uploadResume(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        Part resume = req.getPart("resume");
        InputStream inputStream = resume.getInputStream();
        // 文件名+文件后缀
        String submittedFileName = resume.getSubmittedFileName();
        System.out.println(submittedFileName);
        // 有文件上传
        if (submittedFileName != null) {
            String fileName = StringUtil.uuid() + submittedFileName.substring(submittedFileName.lastIndexOf(".")); //文件名（uuid）+ 文件后缀
            String url = QnyUtil.upload(inputStream, fileName);
            try {
                boolean success = resumeService.saveResume(submittedFileName,url);
                return success ? Result.ok("上传简历成功") : Result.error("上传简历失败");
            } catch (RuntimeException e) {
                e.printStackTrace();
                return Result.error(e.getMessage());
            }

        } else {
            return Result.error("上传简历失败");
        }
    }



    /**
     * 更改头像
     * @param req
     * @param resp
     * @return
     * @throws ServletException
     * @throws IOException
     */
    protected Result uploadPhoto(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        Part photo = req.getPart("photo");
        InputStream inputStream = photo.getInputStream();
        // 文件名+文件后缀
        String photoName = photo.getSubmittedFileName();
        System.out.println(photoName);
        // 有文件上传
        if (photoName != null) {
            String fileName = StringUtil.uuid() + photoName.substring(photoName.lastIndexOf(".")); //文件名（uuid）+ 文件后缀
            String url = QnyUtil.upload(inputStream, fileName);
            try {
                boolean success = resumeService.updatePhoto(url);
                return success ? Result.ok("更改头像成功") : Result.error("更改头像失败");
            } catch (RuntimeException e) {
                e.printStackTrace();
                return Result.error(e.getMessage());
            }

        } else {
            return Result.error("更改头像失败");
        }
    }

}
